package com.alatus.service.impl;

import com.alatus.constant.Constants;
import com.alatus.mapper.TClueRemarkMapper;
import com.alatus.model.TClueRemark;
import com.alatus.query.ClueRemarkQuery;
import com.alatus.service.ClueRemarkService;
import com.alatus.util.JWTUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ClueRemarkServiceImpl implements ClueRemarkService {
    @Resource
    private TClueRemarkMapper tClueRemarkMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveClueRemark(ClueRemarkQuery clueRemarkQuery) {
        TClueRemark tClueRemark = new TClueRemark();
        tClueRemark.setCreateBy(JWTUtils.parseUserFromJWT(clueRemarkQuery.getToken()).getId());
        tClueRemark.setCreateTime(new Date());
        BeanUtils.copyProperties(clueRemarkQuery,tClueRemark);
        return tClueRemarkMapper.insert(tClueRemark);
    }

    @Override
    public PageInfo<TClueRemark> getClueRemarkByPage(Integer current, ClueRemarkQuery clueRemarkQuery) {
//        设置PageHelper和分页情况
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        List<TClueRemark> list = tClueRemarkMapper.selectClueRemarkByPage(clueRemarkQuery);
        PageInfo<TClueRemark> info = new PageInfo<>(list);
        return info;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteClueRemarkById(Integer id) {
//        修改删除状态法,可恢复
//        TClueRemark tClueRemark = new TClueRemark();
//        tClueRemark.setId(id);
//        tClueRemark.setDeleted(1);
//        return tClueRemarkMapper.updateByPrimaryKeySelective(tClueRemark);
        return tClueRemarkMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateClueRemark(ClueRemarkQuery clueRemarkQuery) {
        TClueRemark tClueRemark  = new TClueRemark();
        BeanUtils.copyProperties(clueRemarkQuery,tClueRemark);
        tClueRemark.setEditTime(new Date());
        tClueRemark.setEditBy(JWTUtils.parseUserFromJWT(clueRemarkQuery.getToken()).getId());
        return tClueRemarkMapper.updateByPrimaryKeySelective(tClueRemark);
    }
}
